<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 8/10/2018
 * Time: 12:02 AM
 */
namespace Admin\Controller;

class ManagerController extends BaseController
{
    private $manager = null;

    public function _initialize()
    {
         parent::_initialize();
         $this->manager = D('Manager');
    }

    //修改密码
    public function change()
    {
        if (IS_POST) {
            $old_password = I('post.old_password');
            $new_password = I('post.new_password');
            if ($this->manager->changePassword($old_password, $new_password)) {
                $this->ajaxReturn('true');
            } else {
                $this->ajaxReturn($this->manager->getError());
            }
        }
        $this->display('Manager/change');
    }

    //管理员列表
    public function managerList()
    {
        //不获取超级管理员  --admin为超级管理员
        $managers = $this->manager->select();
        foreach ($managers as $key => $manager) {
            if ($manager['is_default'] == 0) {
                $managers[$key]['is_default'] = '超级管理员';
            } else {
                $managers[$key]['is_default'] = '普通管理员';
            }
            if ($manager['status'] == 1) {
                $managers[$key]['status'] = '是';
            } else {
                $managers[$key]['status'] = '否';
            }
            $managers[$key]['last_time'] = date('Y-m-d H:i:s', $manager['last_time']);
        }
        $this->assign(array(
            'managers' =>$managers
        ));
        $this->display('Manager/list');
    }

    //添加管理员
    public function managerAdd()
    {
        if (!IS_POST) {
            $this->display('Manager/add');
            exit;
        }
        if ($this->manager->managerAdd()) {
            $this->ajaxReturn('true');
        } else {
            $this->ajaxReturn($this->manager->getError());
        }
    }

    //修改管理员
    public function managerModify()
    {
        if (!IS_POST) {
            $manager = $this->manager->where(array('id' => array('eq',I('get.id'))))->find();
            $this->assign('manager', $manager);
            $this->display('Manager/edit');
            exit;
        }

        if ($this->manager->managerModify(I('post.id'))) {
            $this->ajaxReturn('true');
        } else {
            $this->ajaxReturn($this->manager->getError());
        }

    }

    public function managerDel()
    {
        $id = I('get.id');
        $del_manager =$this->manager->where(array('id' => array('eq', $id)))->find();
        $manager = $this->manager->where(array('id' => array('eq', session('id'))))->find();

        if ($manager['is_default'] == 1) {
            $this->ajaxReturn('您没有权限删除管理员账号');
        }

        if ($manager['id'] == $id) {
            $this->ajaxReturn('您不能删除自己的账户');
        }

        if ($del_manager['is_default'] == 0) {
            $this->ajaxReturn('您不能删除其他的超级管理员');
        }

        $result = $this->manager->delete($id);
        if ($result) {
            $this->ajaxReturn('true');
        } else {
            $this->ajaxReturn($this->manager->getError());
        }
    }

}